package com.ruoyi.web.controller.task;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.YcTask;
import com.ruoyi.domain.YcTaskScore;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IYcPersonalService;
import com.ruoyi.service.IYcTaskScoreService;
import com.ruoyi.service.IYcTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 课题Controller
 * 管理员使用
 *
 * @author ruoyi
 * @date 2020-09-08
 */
@Controller
@RequestMapping("/ruoyi/taskadmin")
public class YcTaskAdminController extends BaseController {
    private String prefix = "ruoyi/task";

    private final static String ALL="全部";

    @Autowired
    private IYcTaskService ycTaskService;

    @Resource
    private IYcPersonalService ycPersonalService;

    @RequiresPermissions("ruoyi:task:viewadmin")
    @GetMapping()
    public String task() {
        return prefix + "/taskadmin";
    }

    /**
     * 管理员看到的页面
     */
    @RequiresPermissions("ruoyi:task:listadmin")
    @PostMapping("/admin")
    @ResponseBody
    public TableDataInfo admin(YcTask ycTask) {
        startPage();
        //ALL代表全部,如果搜索框内是全部就给传一个空值
        if (ALL.equals(ycTask.getProjectLevel())){
            ycTask.setProjectLevel(null);
            ycTask.setUserId(ShiroUtils.getUserId());
            List<YcTask> list = ycTaskService.selectYcTaskListAdmin(ycTask);
            return getDataTable(list);
        }
        //管理员只能看到待审批和审批通过的,已备案,待备案 驳回或者撤回的将看不到
        List<YcTask> list = ycTaskService.selectYcTaskListAdmin(ycTask);
        return getDataTable(list);
    }

    /**
     * 驳回备案
     */
    @RequiresPermissions("ruoyi:task:refusal")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping("/refusal")
    @ResponseBody
    public AjaxResult refusal(Long id) {
        //更新备案为驳回备案
        ycTaskService.refusal(id);
        return success();
    }


    /**
     * 通过备案
     */
    @RequiresPermissions("ruoyi:task:consent")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping("/consent")
    @ResponseBody
    public AjaxResult consent(Long id) {
        //更新备案状态为已备案
        ycTaskService.consent(id);
        return success();
    }


    /**
     * 通过审批
     */
    @RequiresPermissions("ruoyi:task:beApproved")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping("/beApproved")
    @ResponseBody
    public AjaxResult beApproved(Long id) {
        //更新审批状态为已通过
        ycTaskService.updateBeApproved(id);
        return success();
    }


    /**
     * 驳回审批
     */
    @RequiresPermissions("ruoyi:task:dismissTheApproval")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping("/dismissTheApproval")
    @ResponseBody
    public AjaxResult dismissTheApproval(Long id) {
        //更新审批状态为驳回
        ycTaskService.updateDismissTheApproval(id);
        return success();
    }


    /**
     * 查看详情人
     */
    @RequiresPermissions("ruoyi:task:detail")
    @GetMapping("/detail/{userId}")
    public String detail(@PathVariable("userId") Long userId, ModelMap mmap) {
         mmap.put("list",ycPersonalService.selectYcPersonalById(userId));
         return prefix + "/personalInformation";
    }

}
